home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: Assembler / C++ class not working!
- Date: 23 Feb 1996 22:07:43 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4gldrf$f29@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe15.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Feb 23, 1996 01:55:06 in article <Assembler / C++ class not working!>,
- 'csmecher@unixg.ubc.ca (Alec or Graeme Smecher)' wrote:
-
-
- > I have defined a class and, within it, several unsigned int's and a
- >few functions. The functions have imbedded assembler in them. If I
- >don't use assembler, I can access these variables in the function just
- >fine. If I introduce imbedded assembler, it's as if the variables
- >aren't there! What is wrong? Here is a condensed version: (Sorry if
- >there's any typos)
- >
- >class MyClass {
- > public:
- > unsigned int a;
- > void function();
- > };
- >void MyClass::function () {
- > asm {
- > mov ax,a
- > ... Do stuff ...
- > }
- > }
- >
- >For some reason, this will not compile!!!
- >Please help!!!
-
- Many compilers use a register for keeping track of the "this" pointer.
- References to data members of the object then are just offsets
- from the address in this register. For example, if you code
-
- int foo = bar;
-
- where bar is a data member, the compiler generates something
- like:
-
- mov eax,dword ptr[ebx+4];
-
- where [ebx+4] contains a pointer to the data member. When you
- reference a data member in assembler code, the assembler is not
- "smart" enough to know that bar is not a an address but a set of
- instructions to the compiler. Of course, one could argue that the
- compiler should detect the situation and substitute the appropriate
- reference, but the one I know about doesn't.
-
- Your only course of action is to declare a temporary local
- variable to hold the value of the data member. E.g.,
-
- int Myclass::memberfunc ()
- {
- int xbar = bar;
- asm {
- mov eax,xbar
- ....
- }
- }
-
- This is not as expensive as it sounds. One or two extra clock
- cycles is all it costs you,depending on the machine and
- compiler. In any case, negligible.
-
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-